//
//  IJImagesPicker.m
//  iwjw
//
//  Created by ChenJianjun on 2017/1/17.
//  Copyright © 2017年 iwujwu. All rights reserved.
//

#import "IJImagesPicker.h"
#import "TZImagePickerController.h"

@interface IJImagesPicker ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, copy) void(^completion)(NSDictionary<NSString *,id> *info);
@property (nonatomic, strong) UIImagePickerController *imagePickerController;

@end

@implementation IJImagesPicker

- (void)pickImageWithSourceType:(UIImagePickerControllerSourceType)sourceType
                  allowsEditing:(BOOL)allowsEditing
                     completion:(void (^)(NSDictionary<NSString *,id> * _Nullable))completion
{
    self.completion = completion;
    self.imagePickerController.sourceType = sourceType;
    self.imagePickerController.allowsEditing = allowsEditing;
    
    if (self.controller) {
        [self.controller presentViewController:self.imagePickerController animated:YES completion:nil];
    } else {
        [UIViewController ks_presentViewController:self.imagePickerController animated:YES completion:nil];
    }
    
}

- (void)pickerImagesWithMaxImagesCount:(NSInteger)maxImagesCount
                            completion:(void (^)(NSArray<UIImage *> * _Nullable))completion
{
    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:maxImagesCount delegate:nil];
    picker.allowTakePicture = NO;
    picker.allowPickingVideo = NO;
    picker.isSelectOriginalPhoto = YES;
    picker.sortAscendingByModificationDate = NO;
    picker.barItemTextColor = [BLColorConfig blackColor];
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos,NSArray *assets,BOOL isSelectOriginalPhoto) {
        if (photos.count != 0
            && completion) {
            completion(photos);
        }
    };
    if (self.controller) {
        [self.controller presentViewController:picker animated:YES completion:nil];
    } else {
        [UIViewController ks_presentViewController:picker animated:YES completion:nil];
    }
}

- (void)pickerVideoCompletion:(void (^)(UIImage *coverImage,id asset))completion
{
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4  delegate:nil pushPhotoPickerVc:YES];
//    imagePickerVc.autoDismiss = NO;
    imagePickerVc.isSelectOriginalPhoto = NO;
    imagePickerVc.allowTakePicture = NO;
    imagePickerVc.allowPickingVideo = YES;
    imagePickerVc.allowPickingImage = NO;
    imagePickerVc.allowPickingOriginalPhoto = YES;
    imagePickerVc.maxImagesCount = 1;
    imagePickerVc.sortAscendingByModificationDate = YES;
    imagePickerVc.alwaysEnableDoneBtn = NO;
    imagePickerVc.barItemTextColor = [BLColorConfig blackColor];
    
//    imagePickerVc.showSelectBtn = NO;
//    imagePickerVc.allowCrop = NO;
//    @weakify(imagePickerVc);
//    [imagePickerVc setImagePickerControllerDidCancelHandle:^{
//        @strongify(imagePickerVc);
////        [self.imagePickerController hideProgressHUD];
//        [imagePickerVc dismissViewControllerAnimated:YES completion:nil];
//    }];

    
//    TZImagePickerController *picker = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
//    picker.allowTakePicture = NO;
//    picker.allowPickingVideo = NO;
//    picker.isSelectOriginalPhoto = YES;
//    picker.sortAscendingByModificationDate = NO;
//    picker.barItemTextColor = [BLColorConfig blackColor];
    
    imagePickerVc.didFinishPickingVideoHandle = ^(UIImage *coverImage, id asset) {
        if (coverImage && completion) {
            completion(coverImage, asset);
        }
    };
    
    if (self.controller) {
        [self.controller presentViewController:imagePickerVc animated:YES completion:nil];
    } else {
        [UIViewController ks_presentViewController:imagePickerVc animated:YES completion:nil];
    }
}


#pragma mark -

- (UIImagePickerController *)imagePickerController
{
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
    }
    return _imagePickerController;
}

- (void)completionImagePick:(UIImagePickerController *)picker info:(NSDictionary *)info
{
    if (self.completion) {
        self.completion(info);
    }
    if (picker) {
        [picker dismissViewControllerAnimated:YES
                                   completion:nil];
    }
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self completionImagePick:picker info:info];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self completionImagePick:picker info:nil];
}

@end
